﻿using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;

class BuildFairyGUIAssetBundles
{
    [MenuItem("Window/Build FairyGUI Bundles")]
    public static void Builde()
    {
        string _UIPath = Application.dataPath+ "/Resources/UI";
        DirectoryInfo _folder = new DirectoryInfo(_UIPath);
        string _PackageName;
        Dictionary<string, List<string>> PackageName = new Dictionary<string, List<string>>();
        foreach (FileInfo f in _folder.GetFiles("*.bytes"))
        {
            _PackageName = f.Name.Replace("_fui.bytes","");
            List<string> temp = new List<string>();
            foreach(FileInfo f2 in _folder.GetFiles("*.png"))
            {
                if (f2.Name.Contains(_PackageName))
                    temp.Add(f2.Name);
            }
            PackageName.Add(f.Name, temp);
        }
        foreach(string key in PackageName.Keys)
        {
            List<string> _temp = PackageName[key];
            string abresname = key.Replace("fui.bytes", "res.ab");
            string abcfgname = key.Replace("fui.bytes","cfg.ab");
            foreach (string s in _temp)
            {
                AssetImporter.GetAtPath($"Assets/Resources/UI/{s}").assetBundleName=$"UI/{abresname}";
            }
            AssetImporter.GetAtPath($"Assets/Resources/UI/{key}").assetBundleName = $"UI/{abcfgname}";
        }
        BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath,BuildAssetBundleOptions.ChunkBasedCompression,BuildTarget.StandaloneWindows64);
        Debug.Log("ab包打包完成");
    }
}

   

